home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Dev / misc / temgen.lha / Temgen / tg-0.11 / lintab.c < prev    next >
C/C++ Source or Header  |  2002-12-18  |  2KB  |  82 lines

  1. #include "lintab.h"
  2. #include "alloc.h"
  3. #include "sysdefs.h"
  4.  
  5. struct lintab {
  6.     void **data;
  7.     int size, delta, maxndx;
  8. };
  9.  
  10. struct lintab *new_lintab( int size, int delta )
  11. {
  12.     struct lintab *t;
  13.     
  14.     if ( size < 0 || delta < 0 ) return 0;
  15.     
  16.     t = (struct lintab*)MALLOC( sizeof(*t) );
  17.     if ( t ) {
  18.         t->size = size;
  19.         t->delta = delta;
  20.         t->maxndx = -1;
  21.         if ( t->size ) {
  22.             t->data = (void**)CALLOC( t->size, sizeof(void*) );
  23.             if ( !t->data ) t->size = 0; 
  24.         }
  25.     }
  26.     
  27.     return t;
  28. }
  29.  
  30. void free_lintab( struct lintab *t )
  31. {
  32.     if ( t ) {
  33.         if ( t->data ) FREE( t->data );
  34.         FREE( t );
  35.     }
  36. }
  37.  
  38. int lt_set( struct lintab *t, int n, void *p )
  39. {
  40.     if ( !t ) return -1;
  41.     if ( n < 0 ) return -1;
  42.    
  43.     if ( !p ) {
  44.             if ( n < 0 || n > t->maxndx ) return 0;
  45.             t->data[ n ] = NULL;
  46.             return 0;
  47.     }
  48.     
  49.     if ( n >= t->size ) {
  50.         int s1, s2;
  51.         void **old = t->data;
  52.         s1 = t->size + t->delta;
  53.         s2 = n+1;
  54.         s1 = (s1>s2) ? s1: s2;
  55.         t->data = (void**)REALLOC( old, s1*sizeof(t->data[0]) );
  56.         if ( !t->data ) {
  57.             t->data = old;
  58.             return -1;
  59.         }
  60.         
  61.         memset( t->data+t->size, 0, (s1-t->size) * sizeof(t->data[0]) );
  62.         t->size = s1;
  63.     }
  64.     
  65.     t->data[ n ] = p;
  66.     if ( n > t->maxndx ) t->maxndx = n;
  67.     return 0;
  68. }
  69.  
  70. void *lt_get( struct lintab *t, int n )
  71. {
  72.     if ( !t ) return NULL;
  73.     if ( n < 0 || n > t->maxndx ) return NULL;
  74.     return t->data[ n ];
  75. }
  76.  
  77. int lt_maxindex( struct lintab *t )
  78. {
  79.     return t ? t->maxndx: -1;
  80. }
  81.  
  82.